﻿@section header
{
    <link href="~/Themes/RongJi/Style/base/css/shoppingCart.css" rel="stylesheet">
}
@using Kevin.Shop.Business.Service.Entity.Helper
@model RiderShop.Mobile.Modle.Purchase.ShippingCartModel

@section title
{
    购物车
}

 <div id="wizard">
    <ul class="anchor">
        <li id="step-1">
            <a rel="1" class="selected">Step 1 查看我的购物车</a>
        </li>
        <li id="step-2">
            <a rel="2" class="disabled">Step 2 填写核对订单信息</a>
        </li>
        <li id="step-3">
            <a rel="3" class="disabled">Step 3 购买成功</a>
        </li>
    </ul>
    <div class="shoppingCartWrapper">
        <span class="icon lan"></span>
        <h2 class="StepTitle">我的购物车</h2>
        <table id="tableRepList">
            <thead>
                <tr>
                    <th></th>
                    <th>商品名称</th>
                    <th>单价</th>
                    <th>数量</th>
                    @*<th>限购</th>*@
                    <th>小计</th>
                    <th>库存</th>
                    <th>操作</th>
                </tr>
            </thead>
            <tbody>
                @foreach (var itemInfo in Model.CartInfo.CommonProductListNoCOM)
                {

                    <tr>
                        <td class="img">
                            <a href="~/Product/ProductDetail?prd=@itemInfo.ProductId">
                                <img src="@itemInfo.ImageName" alt="@itemInfo.Title">
                            </a>
                        </td>
                        <td class="pro">
                            <a href="~/Product/ProductDetail?prd=@itemInfo.ProductId" title="@itemInfo.Title">@itemInfo.Title</a>
                        </td>
                        <td class="price">@itemInfo.UnitPrice</td>
                        <td class="amt" name="qtyOp" pid="@itemInfo.ProductId">
                            <i class="fa fa-minus" onclick="DeclineToCart(this, '@itemInfo.ProductId')"></i>
                            <input type="text" id="quantity" maxlength="3" value="@itemInfo.Quantity" pid="@itemInfo.ProductId" class="cmnTInput">
                            <i class="fa fa-plus" onclick="IncreaseToCart(this, '@itemInfo.ProductId')"></i>
                        </td>
                        @*<td class="amtMax">@itemInfo.LimitQuantity</td>*@
                        <td class="summary" id="summary">@itemInfo.SubTotal</td>
                        <td class="storage">@Html.Raw(itemInfo.InventoryLabel)</td>
                        <td class="oper">
                            <a onclick="KevinShop.Common.ShoppingCart.DeletePrdFromCart('@itemInfo.ProductId');" class="btn_red" href="ShoppingCart"><i class="fa fa-trash"></i> 删除</a>
                        </td>
                    </tr>

                }
            </tbody>
        </table>

        @if (Model.CartInfo.GiftList != null && Model.CartInfo.GiftList.Count > 0)
        {
            <p>赠送您的礼品</p>
            <table width="100%" cellspacing="0" cellpadding="0" border="0" class="giftList">
                <tbody id="divRepGiftList">

                    <tr>
                        <th width="390" class="lb tc">
                            礼品名称
                        </th>
                        <th width="210">
                            礼品数量
                        </th>
                        <th width="217">
                            奖品价值
                        </th>
                        <th width="169" class="rb">
                            操作
                        </th>
                    </tr>

                    @foreach (var itemInfo in Model.CartInfo.GiftList)
                    {


                        <tr>
                            <td>
                                <a href="#" title="@itemInfo.Title">@itemInfo.Title</a>
                            </td>
                            <td>
                                @itemInfo.Quantity
                            </td>
                            <td>
                                @itemInfo.SubTotal
                            </td>
                            <td class="oper">
                                <a href="javascript:void(0)">删除-让用户自己删赠品？</a>
                            </td>
                        </tr>
                    }

                </tbody>
            </table>
        }

        <div class="Cartsummary">
            商品重量：<span id="Weight">@Model.CartInfo.TotalWeight.ToString("0.00")</span>kg
            商品数量：<span id="TotalQuantity">@Model.CartInfo.TotalQuantity</span>件 <b></b>
            您共节省：<strong><span id="DiscountAmount" class="green">￥@Model.CartInfo.DiscountAmount.ToString("0.00")</span></strong>
            总计金额：<span class="finalPrice red" id="TotalAmount">￥@Model.CartInfo.PayAmount.ToString("0.00")</span>
        </div>

        <div class="actionBar">
            <button type="button" class="checkoutBtn" onclick="checkout()">去结算 <i class="fa fa-chevron-right"></i></button>
            <a href="~/" class="goOnBtn"><i class="fa fa-caret-left"></i> 继续购物</a>
            <span class="allPayM">总计金额：<span id="TotalAmountS" class="red">￥@Model.CartInfo.PayAmount.ToString("0.00")</span></span>
            <span id="lbSubmitting"><i class="fa fa-refresh fa-spin"></i> 正在提交数据,请稍候...</span>
        </div>

    </div>
</div>
@section footer
{

    <script language="javascript" type="text/javascript">
        $(document).ready(function() {
            $('#lbSubmitting').hide();
            $('.amt .cmnTInput').each(function(i, item) {
                $(item).focusout(function() {
                    var td = $(item).parents();
                    var pid = $(item).attr('pid');
                    var qty = $(item).val();
                    if (!CheckQuantity(qty))
                        return;
                    ChangeToCart(pid, qty, td);
                });
            });

            //COM不可编辑
            /*
            $("#tableRepList [name='qtyOp']").each(function(i,item) {

            });
            */

            KevinShop.Common.ShoppingCart.RefreshTotalQty();
            var id = $.query.get('pId');
            var qty = $.query.get('qty');
            if (id && qty) {
                KevinShop.MessageBox.Success("成功加入购物车");
            }
        });

        checkout = function() {
            if (KevinShop.Common.ShoppingCart.TotalQty > 0 && islog == 'False') {
                openMiniLoginWindow();
                return;
            }
            if (KevinShop.Common.ShoppingCart.TotalQty == 0) {
                KevinShop.MessageBox.Warning("购物车至少得有一个商品");
                return;
            }
            $('#lbSubmitting').show();
            KevinShop.Common.Ajax.Post('PreCheckProduct','',function(data) {
                            
                $('#lbSubmitting').hide();
                if (!data.Status) {
                    KevinShop.MessageBox.Warning(data.ErrorMessage);
                    return;
                }
                window.location.href = "CheckOut";
            });
        };

        //检查是否是COM
        CheckCOM = function(pid) {
            if (pid == "COM")
                return true;
            return false;
        };

        IncreaseToCart = function(obj, pid) {
            if (CheckCOM(pid))
                return;
            var td = $(obj).parents();
            var qty = parseInt($(td).children('#quantity').first().val());
            if (!CheckQuantity(qty))
                return;
            CheckQuantityForStore(pid, qty, td);
        };
        DeclineToCart = function(obj, pid) {
            if (CheckCOM(pid))
                return;
            var td = $(obj).parents();
            var qty = parseInt($(td).children('#quantity').first().val());
            if (!CheckQuantity(qty))
                return;
            if (qty == 1) {
                KevinShop.MessageBox.Warning("购买数量不能为零");
                return;
            }
            qty--;
            ChangeToCart(pid, qty, td);
        };

        ChangeToCart = function(pid, qty, td) {
            UpdateCart(pid, qty);
            Refresh(td, qty);
        };

        CheckQuantity = function(qty) {
            if (isNaN(qty)) {
                KevinShop.MessageBox.Warning("必须输入有效的数量");
                return false;
            }
            if (qty < 0) {
                KevinShop.MessageBox.Warning("必须输入有效的数量");
                return false;
            }
            return true;
        };

        CheckQuantityForStore = function(pid, qty, td) {
            //库存和限购检查
            KevinShop.Common.Ajax.Post('PreCheckProduct','',function(data) {

                if (!data.Status) {
                    KevinShop.MessageBox.Warning(data.ErrorMessage);
                    return;
                }
                qty++;
                ChangeToCart(pid, qty, td);
            });
            return;
        };
        Refresh = function(td, qty) {
            var tr = $(td).parents();
            var price = $(tr).children('td.price').first().text();
            var lb_sum = $(tr).children('#summary').first();
            $(lb_sum).text((qty * price).toFixed(2));
            var lb_qty = $(td).children('#quantity').first();
            $(lb_qty).val(qty);

        };
        UpdateCart = function(pid, qty) {
            //alert('pid:' + pid + ';' + 'qty:' + qty);
            KevinShop.Common.ShoppingCart.UpdateToCartForQty(pid, qty);
            KevinShop.Common.Ajax.Post("GetShippingCartInfo", "", function(currentData) {
                if (!currentData) {
                    
                    return;
                }
                $('#Weight').text((currentData['TotalWeight']).toFixed(2));
                $('#TotalQuantity').text((currentData['TotalQuantity']).toFixed(0));
                $('#DiscountAmount').text('￥' + (currentData['DiscountAmount']).toFixed(2));
                $('#TotalAmount').text('￥' + (currentData['TotalAmount']).toFixed(2));
                $('#TotalAmountS').text('￥' + (currentData['TotalAmount']).toFixed(2));
                checkGiftTable(currentData['GiftCount']);

                @**       这里的功能后台还没有实现       *@
                //var page = $("#divRepGiftList");
                //page.empty();
                //page.html(currentData['StrRepeaterGiftList']);
            });
        };

        checkGiftTable = function (count) {
            //有无赠品
            if ( count == 0)
                $('#tdgift').hide();
            else {
                $('#tdgift').show();
            }
        };

        var islog = @WebClientContext.Current.IsAuthenticated.ToString().ToLower();
    </script>
}